package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.common.cache.GmallCache;
import com.atguigu.gmall.product.mapper.BaseAttrInfoMapper;
import com.atguigu.gmall.product.model.BaseAttrInfo;
import com.atguigu.gmall.product.model.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class BaseAttrInfoServiceImpl extends ServiceImpl<BaseAttrInfoMapper, BaseAttrInfo> implements BaseAttrInfoService {
    @Autowired
    private BaseAttrValueService baseAttrValueService;
    /**
     * 根据多个 三级分类ID 查询当前分类包含平台属性名列表
     *
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseAttrInfo> getAttrInfoList(Long category1Id, Long category2Id, Long category3Id) {
        //获得持久层对象,自定义方法
        BaseAttrInfoMapper baseAttrInfoMapper = this.getBaseMapper();
        List<BaseAttrInfo> baseAttrInfoList= baseAttrInfoMapper.getAttrInfoList(category1Id,category2Id,category3Id);

        return baseAttrInfoList;
    }

    /**
     * 保存/修改 平台属性以及平台属性值
     *  判断新增平台是否存在   ,,  修改和新增共用一个接口,所以不判断了
     *  判断传过来的对象id是否为空, 空就是 新增,反之修改
     *  新增平台属性记录到平台属性表base arttr info表
     *  批量新增平台属性值记录到base atttr value表
     *  修改,,::  先删属性值,,再增加
     *  保证事务一致性加注解    @Transactional
     * @param baseAttrInfo
     * @return
     */
    @Transactional( rollbackFor = Exception.class)
    @Override
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {
        if (baseAttrInfo.getId()==null){//新增
            this.save(baseAttrInfo); //主键回查
            List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
            if (!CollectionUtils.isEmpty(attrValueList)){
                attrValueList.stream().forEach(baseAttrValue ->
                        baseAttrValue.setAttrId(baseAttrInfo.getId()) );
            }
            //保存集合,批量保存  inert into xxx values(?,?)
            baseAttrValueService.saveBatch(attrValueList);

        }else {
            this.updateById(baseAttrInfo);
            LambdaQueryWrapper<BaseAttrValue> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(BaseAttrValue::getAttrId,baseAttrInfo.getId());
            baseAttrValueService.remove(queryWrapper);
            List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
            if (!CollectionUtils.isEmpty(attrValueList)){
                attrValueList.stream().forEach(baseAttrValue -> baseAttrValue.setAttrId(baseAttrInfo.getId()) );
            }
            //保存集合,批量保存  inert into xxx values(?,?)
            baseAttrValueService.saveBatch(attrValueList);
        }
    }

    /**
     * 根据平台属性ID查询平台属性值集合,,,,然后修改
     *
     * @param attrId
     * @return
     */
    @Override
    public List<BaseAttrValue> getAttrValueList(Long attrId) {
        LambdaQueryWrapper<BaseAttrValue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseAttrValue::getAttrId,attrId);
        List<BaseAttrValue> baseAttrValueList = baseAttrValueService.list(queryWrapper);
        return baseAttrValueList;
    }

    /**
     * 根据商品skuid查询当前水库商品所有平台属性名称(包含属性值)
     *
     * @param skuId
     * @return
     */
    @Override
    @GmallCache(prefix = "attrList:")
    public List<BaseAttrInfo> getAttrList(Long skuId) {
        BaseAttrInfoMapper baseMapper1 = this.getBaseMapper();
        List<BaseAttrInfo> list= baseMapper1.getAttrListBySkuId(skuId);
        //前端渲染平台属性 attrName展示平台属性名称  attrValue展示平台属性值
        list.stream().forEach(lists->{
            List<BaseAttrValue> attrValueList = lists.getAttrValueList();
            if (!CollectionUtils.isEmpty(attrValueList)){
                //某个SKU商品只要新增时候,选择一个属性值,最多一个SKU有一个平台属性值
                lists.setAttrValue(attrValueList.get(0).getValueName());
            }
        });

        return list;
    }
}
